// ES6，使用 babel 编译，需要在 .babelrc 文件中定义 presets

import { gulp, src, dest } from 'gulp'
import yargs from 'yargs'
// import sass from 'gulp-sass'
import GulpCleanCss from 'gulp-clean-css'
import gulpIf from 'gulp-if'
import rename from 'gulp-rename'
import notify from 'gulp-notify'
import sourcemaps from 'gulp-sourcemaps'
import postcss from 'autoprefixer'
import autoprefixer from 'autoprefixer'

const sass = require('gulp-sass')(require('sass'))

const PRODUCTION = yargs.argv.prod

export const main = done => {
    console.log("default task")
    console.log("当前环境为：", PRODUCTION)
    done()
}

export const promise = () => {
    return new Promise(function(resolve, reject) {
        setTimeout(function(){
            resolve()
        }, 300)
    })
}

export const styles = async () => {
    return src('src/scss/**/*.scss')
    .pipe(sourcemaps.init())
    .pipe(sass({
        errLogToConsole: true,
        sync: true,
    }).on('error', sass.logError))
    .pipe(gulpIf(PRODUCTION, GulpCleanCss({compatibility:'ie8'})))
    .pipe(gulpIf(PRODUCTION, sourcemaps.write()))
    .pipe(rename({suffix: ".min"}))
    .pipe(dest('dist/css'))
    .pipe(
        notify({
            message: '\n\n✅  ===> STYLES — completed!\n',
            onLast: true
        })
    )
    await console.log("finish")
}

export default main